package com.gsdse.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.exceptions.ClientException;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

public class AliOSSUtils {

    // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
    private static String endpoint = "https://oss-cn-guangzhou.aliyuncs.com";
    private static String accessKeyId="LTAI5tGgxwHj3K8WSkLTC3Jo";
    private static String accessKeySecret="Cd0PPXFamIqO2ZMrSq55ZuxNSZoAQ2";
    // 填写Bucket名称，例如examplebucket。
    private static String bucketName = "kjrecruit";

    public static String uploadFile(MultipartFile file ,String fileName,String fileType) throws ClientException, IOException {
       InputStream inputStream = file.getInputStream();

        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        //上传到对应文件夹
        ossClient.putObject(bucketName,fileType+"/"+fileName,inputStream);

        String url=endpoint.split("//")[0]+"//"+bucketName+"."+endpoint.split("//")[1]+"/"+fileType+"/"+fileName;
        return url;
    }
}
